package cn.itsource.pethome.org.controller;


import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.base.util.JsonResult;
import cn.itsource.pethome.base.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 添加
     * @param
     * @return
     */
/*    @RequestMapping(value = "/save",method = RequestMethod.PUT)*/
    @PutMapping
    @ResponseBody
    public JsonResult save(@RequestBody Employee employee){
        try {
            employeeService.add(employee);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "添加失败！");
        }
    }

    /**
     * 修改
     * @param
     * @return
     */
/*    @RequestMapping(value = "/update",method = RequestMethod.POST)*/
    @PostMapping
    @ResponseBody
    public JsonResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "修改失败！");
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
/*    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)*/
    @DeleteMapping("{id}")
    @ResponseBody
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            employeeService.removeById(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败!!");
        }
    }
    /**
     *  批量删除
     * @param
     * @return
     */
/*    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)*/
    @PatchMapping("/patchdelet")
    @ResponseBody
    public JsonResult  patchdelet(@RequestBody Long[] ids){
        try {
            employeeService.patchdelet(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败!!");
        }
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
/*    @RequestMapping(value = "/loadbyid",method = RequestMethod.GET)*/
    @GetMapping("{id}")
    @ResponseBody
    public Employee loadbyid(@PathVariable("id") Long id){
            return employeeService.loadbyid(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
/*    @RequestMapping(value = "/loadall",method = RequestMethod.PATCH)*/
    @PatchMapping("/patchAll")
    @ResponseBody
    public List<Employee> loadAll(){
        List<Employee> loadall = employeeService.loadall();
        return  loadall;
    }

    /**
     * 分页查询所有
     * @return
     */
/*    @RequestMapping(value = "/loadall",method = RequestMethod.PATCH)*/
    @PatchMapping
    @ResponseBody
    public PageBean<Employee> loadPage(@RequestBody EmployeeQuery query){
        PageBean<Employee> loadPage = employeeService.loadPage(query);
        return  loadPage;
    }

}
